package com.study.kafka.producer;

import com.study.kafka.message.Demo01Message;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.support.SendResult;
import org.springframework.stereotype.Component;
import org.springframework.util.concurrent.ListenableFuture;

import java.util.concurrent.ExecutionException;

/**
 * @author yuantongqin
 * 2021/8/4
 **/
@Component
public class Demo01Producer {

    Logger logger = LoggerFactory.getLogger(Demo01Producer.class);

    @Autowired
    KafkaTemplate<Object, Object> kafkaTemplate;

    public SendResult<Object, Object> sendSync() throws ExecutionException, InterruptedException {
        long l = System.currentTimeMillis() / 1000;
        Demo01Message message = new Demo01Message();
        message.setId((int) l);

        ListenableFuture<SendResult<Object, Object>> send = kafkaTemplate.send(Demo01Message.topic, message);
        SendResult<Object, Object> result = send.get();
        return result;
    }

    public ListenableFuture<SendResult<Object, Object>> sendAsync() {
        long l = System.currentTimeMillis() / 1000;
        Demo01Message message = new Demo01Message();
        message.setId((int) l);

        ListenableFuture<SendResult<Object, Object>> send = kafkaTemplate.send(Demo01Message.topic, message);
        return send;
    }



}
